package mywebapp.tools.jsf;

public class ValidatorInt
{
  protected static String nl;
  public static synchronized ValidatorInt create(String lineSeparator)
  {
    nl = lineSeparator;
    ValidatorInt result = new ValidatorInt();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = NL + "package ";
  protected final String TEXT_2 = ".validator;" + NL + "" + NL + "import javax.faces.application.FacesMessage;" + NL + "import javax.faces.component.UIComponent;" + NL + "import javax.faces.context.FacesContext;" + NL + "import javax.faces.validator.Validator;" + NL + "import javax.faces.validator.ValidatorException;" + NL + "import java.util.*;" + NL + "" + NL + "/** Validation des champs inputs des formulaires **/" + NL + "public class IntValidator implements Validator {" + NL + "" + NL + "\t/** Verifie que le champs n'est pas vide */" + NL + "\tpublic void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {" + NL + "\t\tString input = (String) value;" + NL + "\t\tif (input == null || input == \"\") {" + NL + "\t\t\tFacesMessage message = new FacesMessage();" + NL + "\t\t\tmessage.setDetail(\" #{msg.detailError}\");" + NL + "\t\t\tmessage.setSummary(\" #{msg.summaryError}\");" + NL + "\t\t\tmessage.setSeverity(FacesMessage.SEVERITY_ERROR);" + NL + "\t\t\tthrow new ValidatorException(message);" + NL + "\t\t}else{" + NL + "\t\t\ttry { " + NL + "\t\t\t\tint i = Integer.parseInt(input); " + NL + "\t\t\t} " + NL + "\t\t\tcatch (Exception e) { " + NL + "\t\t\t\tFacesMessage message = new FacesMessage();" + NL + "\t\t\t\tmessage.setDetail(\" #{msg.detailErrorInt}\");" + NL + "\t\t\t\tmessage.setSummary(\" #{msg.summaryErrorInt}\");" + NL + "\t\t\t\tmessage.setSeverity(FacesMessage.SEVERITY_ERROR);" + NL + "\t\t\t\tthrow new ValidatorException(message);" + NL + "\t\t\t}" + NL + "\t\t}" + NL + "\t}" + NL + "} ";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     String nameApp = (String) argument; 
    stringBuffer.append(TEXT_1);
    stringBuffer.append(nameApp);
    stringBuffer.append(TEXT_2);
    return stringBuffer.toString();
  }
}
